#include <iostream>
#include <random>
#include "a_star.hpp"
std::vector<std::vector<int>> RandomMap(size_t rows, size_t cols, float obstacleRate)
{
    std::vector<std::vector<int>> map(rows, std::vector<int>(cols, 0));
    std::default_random_engine gen{std::random_device()()};
    std::uniform_real_distribution<float> dist(0, 1);

    for (size_t i = 0; i < rows; ++i) {
        for (size_t j = 0; j < cols; ++j) {
            if (dist(gen) < obstacleRate) {
                map[i][j] = 1;
            }
        }
    }
    return map;
}
int main() {
    // 生成一个10x10的地图，其中0表示可走路径，1表示障碍物
    std::vector<std::vector<int>> map = RandomMap(10, 10, 0.2);
    int rows = map.size();
    if (rows == 0)
        return 1;
    int cols = map[0].size();
    Node::Ptr start = Node::Create(0, 0);
    if (start->x >= 0 && start->x < rows && start->y >= 0 && start->y < cols)
        map[start->x][start->y] = 0;
    else return 1;
    std::vector<Node::Ptr> goals;
    goals.emplace_back(Node::Create(-1, -1));
    goals.emplace_back(Node::Create(5, 5));
    goals.emplace_back(Node::Create(3, 7));
    goals.emplace_back(Node::Create(7, 3));

    for(auto& node : goals){
        if (node->x >= 0 && node->x < rows && node->y >= 0 && node->y < cols)
            map[node->x][node->y] = 0;
    }
    
    std::vector<std::vector<int>> path = Astar(map, start, goals);

    for (const auto &point : path) {
        if (map[point[0]][point[1]])
        {
            std::cerr << "Path crash obstacle!" << std::endl;
            return 1;
        }
        map[point[0]][point[1]] = -1; // 路径点
    }
    map[start->x][start->y] = 2; // 起点
    for(auto& node : goals){
        if (node->x >= 0 && node->x < rows && node->y >= 0 && node->y < cols)
            map[node->x][node->y] = 3; // 终点
    }
    for (const auto& row : map)
    {
        for (const auto& cell : row)
        {
            if (cell == -1)
                std::cout << "* ";
            else if (cell == 1)
                std::cout << "# ";
            else if (cell == 2)
                std::cout << "S ";
            else if (cell == 3)
                std::cout << "E ";
            else
                std::cout << "  ";
        }
        std::cout << std::endl;
    }
    
    return 0;
}
